import { timeFormat } from '../../model/utils/common';

export default {
    namespace: 'oilList',
    state: {
        carNumber: '',
        carList: '',
        duration: 0,
        showType : 'none',
        notice: '',
        list: [],
        userData: {}
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        /**
         * 获取油价提示
         * @param {*} param0 
         * @param {*} param1 
         */
        *getCarList({ payload }, { put  }){
            const carList = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/getCarList',
                }
            });

            if(carList && carList.length){
                // 判断我的车辆列表中是否有
                const hasCar = carList.find(item => item.carNumber === payload.carNumber);
                if(!hasCar){
                    const car = carList[0];
                    wx.setStorageSync('carNumber', car.carNumber);
                    yield put({
                        type: 'init',
                        payload: {
                            carNumber : car.carNumber
                        }
                    });
                }
            }else{
                yield put({
                    type: 'save',
                    payload: {
                        carNumber: '',
                        list: [],
                        userData: {}
                    }
                });
                wx.setStorageSync('carNumber', '');
            }
            yield put({
                type: 'save',
                payload: {
                    carList
                }
            });
  
            
            
        },
        *init({ payload }, { put, call, select }){

            const {carNumber} = payload;
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/getOilList',
                    data: {carNumber}
                }
            });

            console.log('---',result);
            const oilMap  = {};
            // if(result.length <=0){
            //     result.push({
            //         carNumber: '测试车辆',
            //         id: -1,
            //         mileage: 30000,
            //         oilAmount: 40,
            //         oilTime: timeFormat(Date.now()),
            //         price: 8.43,
            //         totalPrice: 200,
            //     });
            //     result.push({
            //         carNumber: '测试车辆',
            //         id: -1,
            //         mileage: 30000,
            //         oilAmount: 40,
            //         oilTime: timeFormat(Date.now()-86400),
            //         price: 8.87,
            //         totalPrice: 300,
            //     });
            // }
            result.forEach(item => {
                const month = timeFormat(item.oilTime, 'YYYY年MM月');
                if(oilMap[month]){
                    oilMap[month].push(item);
                }else{
                    oilMap[month] = [item];
                }
            });

            const list = Object.keys(oilMap).map(key => {
                let totalPrice = 0;
                oilMap[key].forEach(item => totalPrice += parseInt(item.totalPrice*100));
                totalPrice = totalPrice/100;
                return {
                    month: key,
                    totalPrice,
                    list : oilMap[key]
                };
            });
            let duration = 0;
            if(result && result.length){
                duration = Date.now() - new Date(result[0].oilTime).getTime() ;
                duration = (duration/1000/60/60/24).toFixed(1); 
            }
            yield put({
                type : 'save',
                payload : {
                    list,
                    carNumber,
                    duration
                }
            });
            yield put({ type : 'getStatistic', payload: {carNumber}});
        },

        *getStatistic({ payload}, { put }){
            const {carNumber} = payload;
            const userData = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/api/statistic',
                    data: {
                        carNumber
                    }
                }
            });
            yield put({ type: 'save', payload: {userData : userData||{}} });
        },
    }
};